from django import template
from  datetime import datetime

register = template.Library()

from datetime import datetime
from django.conf import settings as my_settings

moneyTypeDict = {
    "1": "月",
    "2": "年",
    "3": "天",
    "4": "时"
}

degreeTypeDict = {
    "0": "不限",
    "1": "初中及以下",
    "2": "高中/中技/中专",
    "3": "大专",
    "4": "本科",
    "5": "硕士",
    "6": "博士",
    "7": "其他"
}

companyTypeDict = {
    "2": "外资",
    "3": "合资",
    "4": "国企",
    "5": "民营",
    "6": "代表处",
    "7": "政府机关(国家机构)",
    "8": "事业单位",
    "9": "非营利机构",
    "10": "上市公司",
    "11": "创业公司",
    "12": "股份制",
    "13": "其他",
    "14": "医院",
    "15": "律师事务所",
    "16": "港澳台公司",
    "17": "银行",
    "18": "外商独资",
    "19": "学校/下级学院",
    "20": "社会团体",
}
oDict = {
    "1": "51job",
    "2": "拉钩",
    "3": "猎聘网",
    "4": "中华英才",
    "5": "58同城",
    "6": "赶集网",
	"7": "智联招聘"
}


@register.filter
def getTitle(value,index):
    return value.split('_')[index]


@register.filter
def getCompanyType(value):
    value = str(value)
    if value in companyTypeDict:
        return companyTypeDict[value]
    return "其他"


@register.filter
def getCrawlFrom(value):
    value = str(value)
    if str(value) in oDict:
        return oDict[value]
    return ""


@register.filter
def getDegreeType(value):
    value = str(value)
    if str(value) in degreeTypeDict:
        return degreeTypeDict[value]
    return ""

@register.filter
def getFilterValue(request,value):
    return request.GET.get("filter_"+value.split("_")[1],"")




@register.filter
def getMoneyType(value):
    value = str(value)
    if value in moneyTypeDict:
        return moneyTypeDict[value]
    return ""


@register.filter
def getMoneyFormat(value, flag=1):
    if value > 1000:
        return "%s%s" % (value / 1000, flag and "K" or "")
    elif value > 10000:
        return "%s%s" % (value / 10000, flag and "W" or "")
    else:
        return "%s%s" % (value, flag and "元" or "")


@register.simple_tag
def getCompanySize(s_size, e_size):
    if e_size == -1:
        return str(s_size) + "人"
    else:
        return str(s_size) + "-" + str(e_size) + "人"


@register.filter
def mod(value, divider):
    try:
        return value % divider
    except:
        return ""


@register.simple_tag
def substring(value, s, e):
    try:
        return value[s:e]
    except Exception as ex:
        return value


@register.filter
def pubTimeFormat(value):
    try:
        # print(datetime.strptime(value, "%Y-%m-%d %H:%M"))
        return value.strftime("%Y-%m-%d")
    except:
        return ""


@register.inclusion_tag("jobsearch/benefit.html")
def benefit(data):
    if data is None or data == "":
        return {"data": []}
    data = data.split(',')
    return {"data": data}
